/**
 * 示例 <div v-if="value.key === 'gmtCreate'">{{scope.row.gmtCreate | parseTimeFilter('{y}-{m}-{d} {h}:{i}:{s}')}}</div>
 * @param time
 * @param cFormat
 * @return {*}
 */
export function parseTimeFilter(time, cFormat) {
  if (time === '' || time === null) {
    return ''
  }
  // 没有定义过滤格式
  if (!cFormat) {
    if ((time + '').length === 13 || (/000$/).test(time)) {
      time = parseInt(time)
      cFormat = '{y}-{m}-{d}'
    }
  }
  time = new Date(time)
  if (arguments.length === 0) {
    return null
  }

  if ((time + '').length === 10) {
    time = +time * 1000
  }

  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    date = new Date(parseInt(time))
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}
// 通用- -  是否拆单/是否回程/是否允许区域合并/是否分片区运营 ; 1-是, 0-否
export function yesOrNoFilter(str) {
  str = parseInt(str)
  if (str === 1) {
    return '是'
  } else if (str === 0) {
    return '否'
  }
}
// 通用- - 无值 取0
export function nullNumberOrZeroFilter(str) {
  if (str) {
    return str
  } else {
    return 0
  }
}
// 通用- 小数转百分比
export function numberToPercent(v) {
  if (v === '' || v === null) {
    return ''
  }
  let number = parseFloat(parseFloat(v) * 100).toFixed(2)
  const str = number.split('.')
  if (str.length === 2) {
    if (str[1] === '00') {
      number = str[0]
    }
  }
  return number + '%'
}
// 通用-时间str去掉 '.000'
export function parseTimeCut000Filter(str) {
 if (str) {
   if (str.indexOf('.000') !== -1) {
     return str.substring(0, str.indexOf('.000'))
   } else {
     return str
   }
 } else {
   return ''
 }
}
